<?php
class LogModel extends CommonModel {
    public function __construct(){
        parent::__construct();
    }
     /*
     *@des 获取log列表 ， table的名字 
     * 
     */
    public function log_table(){
        $query_table = "show tables like '".C('DB_PREFIX')."admin_log%'" ;
        $res_table = M()->query($query_table);

        $table_array = array();
        if($res_table){
            foreach($res_table as $k_t=>$k_v){
                foreach($k_v as $kk=>$vv){
                    $table_array[] = $vv ;
                }
            }
            rsort($table_array) ;
        }
        
        
        return $table_array ;
    }
/*
     *@page 第几页
     *@per_page 每一页显示的数据 
     */
    public function queryLogList($p =  1 ,$per_page , $where = array() ){
        $getwhere  = ' where 1= 1 '; ;
        $table = $where['table'];
        $tablename = '';
        $tableArray = $this->log_table() ; 
        if( in_array($table,$this->log_table())){
            $tablename = $table ;
        }else{
            $tablename = $tableArray[0];
        }
        $status = $where['status'];        
        if(!empty($where['log_person'])){  
            $log_person = $where['log_person'] ; 
            $getwhere.=" AND `log_person` LIKE '%{$log_person}%'";
        }
        if(!empty($where['log_url'])){
            $log_url = $where['log_url'];
            $getwhere.=" AND `log_url` LIKE '%{$log_url}%'";
        }
        if(in_array($status,array('1','0'))){
            $getwhere.=" AND `log_status` = '{$status}'"; 
        }

        import('Library.Page' , APP_PATH);
        $data = array() ;
        $sql_count = "SELECT COUNT(*) AS tt FROM {$tablename} {$getwhere} ";
        $totalData = M()->query($sql_count);
        $total = $totalData[0]['tt'];
        $page = new Page($total,$per_page);
        $page_string =  $page->show();
        if($p <=0 ){
            $p = 1 ;
        }
        $limit = ($p-1)*$per_page;
        $limit.=",{$per_page}";
        
        $sql_log = "SELECT * FROM {$tablename} {$getwhere} order by log_id desc limit  {$limit}";
       // echo $sql_log;
        $list = M()->query($sql_log);
        
        foreach($list as $k=>$v){
            $list[$k]['log_status'] = ($v['log_status'] == 1 )?"成功":'<font color="red">失败</font>';
            $list[$k]['log_sql'] = msubstr($v['log_sql'] , 0 ,10);
            $list[$k]['log_sql_all'] = base64_encode($v['log_sql']) ; 
        }
        $data = array(
            'list'=>$list , 
            'page_sting'=>$page_string      
        );
        return $data ;
    }
  

}
